<template>
	<view class="container">
		<view class="header">
			<view class="subsection">
				<view :class=" index == subsectionCurrent?'subItem subItemCur':'subItem' " @click="subsectionChange(index)" v-for="(item, index) in subsectionList" :key="index">
					{{ item.name }}
				</view>
			</view>
		</view>
		<view class="wrapper" style="height: calc(100% - 110px);">
			<view class="main" >
				<scroll-view class="scroll" :scroll-y="true" :scroll-top="scrollTop" :show-scrollbar="false"
					:refresher-threshold="50" :refresher-triggered="refreshStatus" :lower-threshold="100"
					:scroll-with-animation="true" refresher-enabled="true" @refresherrefresh="onRefreshData"
					@scrolltolower="onLoadData" @scroll="onScroll">
					
					<view class="section" v-for="(item, index) in list" :key="index">
						<view class="card" style="align-items: center;justify-items: center;">
							<u-avatar size="200" shape="square" :src="item.qrCode"></u-avatar>
							<view class="content" style="justify-items: center;align-items: center;width:100%;">
								<view class="pdleft">
									<view class="title" style="min-height: 80rpx;overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;">
										{{ item.deviceName }}
									</view>
									<view  class="desc" style="line-height: 40rpx;">
										设备编号：{{ item.deviceImei }}
									</view>
									<view  class="desc" style="line-height: 40rpx;">
										设备状态：{{ (item.onlineStatus == 1?'在线':'离线') }}
									</view>
									<view  class="desc" style="line-height: 40rpx;">
										租借状态：{{ (item.usageStatus==1?'使用中':'未使用') }}
									</view>
								</view>
							</view>
						</view>
					</view>
					
					<view class="nullData" v-if="page == 1 && list.length <=0">
						<view>
							<view>
								<image src="../../../static/image/notdata.png" class="nullImage"></image>
							</view>
							<view class="nullText">
								暂无记录
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//当前页数
				page: 1,
				//每页显示条数
				size: 10,
				//列表数据
				list: [],
				//总页数
				pages: 0,
				//监听滚动
				scrollTop: 0,
				realScrollTop: 0,
				refreshStatus: false,
				/**当前选择的状态序号**/
				subsectionCurrent: 0,
				/**状态分段**/
				subsectionList: [
					{status: '',name: '全部'},
					{status: '1',name: '使用中'},
					{status: '2',name: '未使用'},
				]
			}
		},
		onLoad() {
			
		},
		onShow() {
			this.fetchList();
		},
		methods: {
			// 设备记录
			fetchList() {
				uni.showLoading({
					title: "加载中",
					mask: true
				})
				var params = {};
				params.current = this.page;
				params.size = this.size;
				params.venueId = uni.getStorageSync('venueId');
				params.usageStatus = this.subsectionList[this.subsectionCurrent].status;
				this.$request({
					url: '/blade-merchant-app/merchant-app/device/venue-device',
					data: params
				}).then(res => {
					const {
						records = [],
							pages
					} = res.data;
					
					if (this.page === 1) {
						this.list = records;
					} else {
						this.list = this.list.concat(records);
					}
					this.pages = pages || 0;
					setTimeout(() => {
						this.refreshStatus = false;
						uni.hideLoading();
					}, 300);
				})
			},
			// 监听滚动事件
			onScroll(e) {
				this.realScrollTop = e.detail.scrollTop;
			},
			// 刷新数据
			onRefreshData() {
				this.refreshStatus = true;
				this.page = 1;
				this.list = [];
				this.fetchList();
			},
			// 加载数据
			onLoadData() {
				if (this.page < this.pages) {
					this.page += 1;
					this.fetchList();
				}
			},
			onLink(path) {
				uni.navigateTo({
					url: path
				})
			},
			//状态栏切换事件
			subsectionChange(e){
				this.subsectionCurrent = e;
				this.onRefreshData();
			},
		}
	}
</script>


<style lang="scss">
	page {
		height: 100%;
		overflow: hidden;
		background: #f1f1f1;

		.container {
			height: 100%;
			
			.header {
				display: flex;
				flex-direction: row;
				padding: 30rpx 0;
				background-color: #ffffff;
				z-index: 1;
			
				.subsection {
					display: flex;
					background: #ffffff; padding: 0rpx 0rpx;
					width: 100%;
					
					.subItem {
						width: 33%;
						font-size: 33rpx;
						text-align: center;
					}
					
					.subItemCur {
						font-weight: 600;
						position: relative;
						padding-bottom:10rpx;
					}
					
					.subItemCur::after {
						position: absolute;
						content: ' ';
						width: 20%;
						height: 6rpx;
						bottom: 0px;
						background: red;
						left: 40%;
					}
					
				}
			}

			.wrapper {
				display: flex;
				flex-direction: column;
				// height: calc(100% - 70px);
				height: 100%;
				

				.main {
					flex: 1;
					height: 100%;

					.scroll {
						height: 100%;

						.section {
							padding: 30rpx 30rpx 30rpx 30rpx;
							background-color: #ffffff;
							margin-top: 30rpx;

							.card {
								display: inline-flex;
								
								.imgs {
									width: 250rpx;height: 250rpx;
								}
								.content {
									width: 100%;
									
									.pdleft {
										padding-left:30rpx;
										.title {
											font-size: 33rpx;font-weight: 600;
										}
										.desc {
											font-size: 26rpx;color: #666666;line-height: 50rpx;
										}
									}
									
									
								}
								
							}

							
						}
					}
				}
			}
		}
		
		.nullData {
			height: 100%;
			display: flex;
			-webkit-box-pack: center;
			justify-content: center;
			-webkit-box-align: center;
			align-items: center;
			
			.nullImage {
				width: 120px;height: 120px;
			}
			.nullText {
				color:#666666;font-size:29rpx;padding-top:15rpx;text-align: center;
			}
			
		}
	}
	
</style>
